//
//  PageContentView.swift
//  CBLearnDY
//
//  Created by ncimall on 2017/11/17.
//  Copyright © 2017年 ncimall. All rights reserved.
//

import UIKit

protocol PageContentViewDelegate: class {
    func pageContentViewResponse(pageContentView: PageContentView, currentIndex: Int)
}

private let ContentCellID = "ContentCellID"

class PageContentView: UIView {
    
    //  MARK:- 定义属性
    fileprivate var childVCs : [UIViewController]
    fileprivate weak var parentVC : UIViewController?
    fileprivate var startOffsetX: CGFloat = 0
    
    weak var delegate: PageContentViewDelegate?
    
    //  MARK:- 懒加载属性
    fileprivate lazy var collectionView : UICollectionView = { [weak self] in
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = (self?.bounds.size)!
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal        //  水平滚动
        
        let collectionView = UICollectionView.init(frame: CGRect.init(), collectionViewLayout: layout)
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: ContentCellID)
        
        return collectionView
    }()
    
    //  MARK:- 自定义构造函数
    init(frame: CGRect, childVCs: [UIViewController], parentVC: UIViewController?) {
        self.childVCs = childVCs
        self.parentVC = parentVC
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//  MARK:- 设置UI界面
extension PageContentView {
    fileprivate func setupUI() {
        //  1.将所有的子控制器添加到父控制器中
        for childVC in childVCs {
            parentVC?.addChildViewController(childVC)
        }
        
        //  2.添加collectionView
        addSubview(collectionView)
        collectionView.frame = bounds
    }
}

//  MARK:- 遵守UICollectionView的dataSource协议
extension PageContentView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVCs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //  1.创建cel
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ContentCellID, for: indexPath)
        
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        
        //  2.给cell设置内容
        let childVC = childVCs[indexPath.item]
        childVC.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childVC.view)
        
        return cell
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX = scrollView.contentOffset.x
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        var progress: CGFloat = 0
        var sourceIndex: Int = 0
        var targetIndex: Int = 0
        
        let currentOffsetX = scrollView.contentOffset.x
        let scrollViewW = scrollView.bounds.width
        progress = currentOffsetX.truncatingRemainder(dividingBy: scrollViewW)
        
        
        if currentOffsetX > startOffsetX {  //  左滑
            
        } else {    //  右滑
            
        }
        
        
        let currentIndex = Int(scrollView.contentOffset.x / kxScreenW)
        delegate?.pageContentViewResponse(pageContentView: self, currentIndex: currentIndex)
    }
}

//  MARK:- 外部暴露的方法
extension PageContentView {
    func setCurrentIndex(currentIndex: Int) {
        let offsetX = CGFloat(currentIndex) * kxScreenW
        collectionView.setContentOffset(CGPoint.init(x: offsetX, y: 0), animated: false)
    }
}
